package com.haois.mp.database.module.offaccounts.controller;

import me.chanjar.weixin.common.exception.WxErrorException;
import com.haois.mp.database.module.basic.controller.BaseController;
import com.haois.mp.database.commom.GetModelAndView;
import com.haois.mp.database.exception.OutException;
import com.haois.mp.database.module.remote.service.INoticeService;
import com.haois.mp.database.module.wxserver.service.TempMessageService;
import com.haois.mp.database.util.mp.DataCheck;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by RuiHaoZ on 2017/12/7.
 */
@Controller
@RequestMapping("/wxmp/info")
public class InfoController extends BaseController {

    private static final Logger logger = LoggerFactory
            .getLogger(GroupController.class);

    @Autowired
    private INoticeService iNoticeService;
    @Autowired
    private TempMessageService tempMsgService;



    @RequestMapping(value = "/wechat")
    public ModelAndView yellowPages(HttpServletRequest request) throws WxErrorException {

        ModelAndView mv = new ModelAndView("/share/wechat");
        mv.addObject("fromUser", DataCheck.getOpenId(request));
        mv.addObject("fromPage", "wechat");
        return mv;
    }

    @RequestMapping(value = "/miniProgram")
    public ModelAndView tipMiniApp(String oid, HttpServletRequest request) throws WxErrorException {
        ModelAndView mv = new ModelAndView("/share/miniApp");
        mv.addObject("fromUser", DataCheck.getOpenId(request));
        mv.addObject("fromPage", "");
        return mv;
    }

    @RequestMapping(value = "/frame")
    public ModelAndView toFrame(String oid, HttpServletRequest request) throws WxErrorException {
        ModelAndView mv = new ModelAndView("/info/frame");
        mv.addObject("oid", oid);
        mv.addObject("fromPage", "");
        return mv;
    }

    @RequestMapping(value = "/frame2")
    public ModelAndView toFrame2(String oid, HttpServletRequest request) throws WxErrorException {
        ModelAndView mv = new ModelAndView("/info/frame2");
        mv.addObject("oid", oid);
        mv.addObject("fromPage", "");
        return mv;
    }

    @RequestMapping(value = "/group")
    public ModelAndView organize(String oid, HttpServletRequest request) throws WxErrorException, OutException {
        ModelAndView mv = new ModelAndView("/info/group");
        mv.addObject("oid", oid);
        mv.addObject("fromPage", "");
        return mv;
    }

    @RequestMapping(value = "/notice")
    public ModelAndView getById(Integer id, HttpServletRequest request) throws WxErrorException, OutException {
        String access_token = (String) request.getAttribute("access_token");
        Map result = iNoticeService.getNoticeById(access_token, id);
        ModelAndView mv = new ModelAndView("/info/notice");
        if (!"00".equals(result.get("code"))) {
            mv = GetModelAndView.get404Page();
        }
        mv.addObject("result", result);
        return mv;
    }

    @RequestMapping(value = "/noGroup")
    public ModelAndView noGroup(HttpServletRequest request) throws WxErrorException, OutException {
        String fromPage = (String) request.getAttribute("fromPage");
        ModelAndView mv = new ModelAndView("/info/noGroup");
        mv.addObject("fromPage", fromPage);
        return mv;
    }

    @RequestMapping(value = "/memOrg")
    public ModelAndView memOrg(String oid, HttpServletRequest request) throws WxErrorException {
        ModelAndView mv = new ModelAndView("/info/authorize");
        mv.addObject("oid", oid);
        mv.addObject("fromPage", "authorize");
        return mv;
    }

    @RequestMapping(value = "/tempMsg")
    public ModelAndView tempMsgId(String id, HttpServletRequest request) throws WxErrorException, OutException {
        Map result = tempMsgService.getTempInfo(id);
        ModelAndView mv = new ModelAndView("/info/tempMsg");
        if (result == null) {
            mv = GetModelAndView.get404Page();
        }
        mv.addObject("result", result);
        mv.addObject("fromUser", DataCheck.getOpenId(request));
        return mv;
    }
}
